//
//  LMHomeController.m
//  舍品
//
//  Created by 楼 眉 on 16/3/10.
//  Copyright © 2016年 楼 眉. All rights reserved.
//

#import "LMHomeController.h"
#import <MJRefresh.h>
#import <SDCycleScrollView.h>
#import <ReactiveCocoa.h>
#import <YYWebImage.h>
#import "LMNormalCell.h"
#import "LMDetailsController.h"
#import "LMWarningLabel.h"

@interface LMHomeController ()<UIScrollViewDelegate>

@property (nonatomic,strong)  NSArray *allArray;

@end

@implementation LMHomeController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarHidden = NO;
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.tableView.separatorStyle = NO;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self setupTitleView];
    [self firstSetupData];
    
    __weak typeof (self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                {
                                    [weakSelf chooseUi];
                                }
                                ];
}
#pragma mark - 根据网络加载UI
- (void)chooseUi
{
    __weak typeof (self) weakSelf = self;
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    if (manager.isReachable == NO) {
        [LMWarningLabel LM_createWarningLabelWithTitle:nil];
        [weakSelf.tableView.mj_header endRefreshing];
    }else
    {
        [self setupData];
    }

}


#pragma mark - 请求数据
- (void)firstSetupData
{
    __weak typeof(self) weakSelf = self;
    [SVProgressHUD showWithStatus:@"加载中..."];
    AVQuery *query = [AVQuery queryWithClassName:@"home"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error)
        {
            weakSelf.allArray = objects;
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        }
        else
        {
            [SVProgressHUD dismiss];
        }
    }];
    

}
-(void)setupData
{
    __weak typeof(self) weakSelf = self;
    AVQuery *query = [AVQuery queryWithClassName:@"home"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error)
        {
            weakSelf.allArray = objects;
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            [weakSelf.tableView.mj_header endRefreshing];
        }
    }];
    
}

#pragma mark - setupTitleView
- (void)setupTitleView
{
    self.navigationItem.title = @"舍品";
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tabbar-light"] forBarMetrics:UIBarMetricsDefault];
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *identifier = @"cell";
    
        LMNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[LMNormalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        AVObject *normal = self.allArray[indexPath.row];
        cell.normal = normal;
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 280*kUnit;
}
#pragma mark - 滚动图片被点击
-  (void)scrollViewTap:(UITapGestureRecognizer *)tap
{
    NSLog(@"我被点击了");
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    AVObject *object = self.allArray[indexPath.row];
    LMDetailsController *newVc = [[LMDetailsController alloc]init];
    newVc.view.backgroundColor = [UIColor whiteColor];
    newVc.home = object;
    [self.navigationController pushViewController:newVc animated:YES];
}

#pragma mark - warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    NSLog(@"dealloc--HomeVc");
}
@end
